package com.itheima.dao.impl;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author wuli
 * @create 2022-01-10 8:00
 */
public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;

    public void save(Account account) {
        jdbcTemplate.update("insert into account(name, money) values (?, ?)", account.getName(), account.getMoney());
    }

    public void update(Account account) {
        jdbcTemplate.update("update account set name = ?, money = ? where id = ?", account.getName(), account.getMoney(), account.getId());
    }

    public void delete(Account account) {
        jdbcTemplate.update("delete from account where id = ?", account.getId());
    }

    public Account findById(Integer id) {
        List<Account> list = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>(), id);
        return list.isEmpty() ? null : list.get(0);
    }

    public List<Account> findAll() {
        return jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<Account>());
    }
}
